/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.*;
import com.cashmate.hibernate.pojo.Areas;
import com.cashmate.hibernate.pojo.Borrowers;
import com.cashmate.hibernate.pojo.Branches;
import com.cashmate.hibernate.pojo.Documents;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.Loanverification;
import com.cashmate.hibernate.pojo.Products;
import com.cashmate.hibernate.pojo.Request;
import com.opensymphony.xwork2.ActionSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Lee
 */
public class UpdateLA extends ActionSupport {

    private BorrowerDAO bord = (BorrowerDAO) ServletActionContext.getServletContext().getAttribute("borrowerDAO");
    private BranchDAO brd = (BranchDAO) ServletActionContext.getServletContext().getAttribute("branchDAO");
    private EmployeeDAO emd = (EmployeeDAO) ServletActionContext.getServletContext().getAttribute("employeeDAO");
    private AreaDAO ard = (AreaDAO) ServletActionContext.getServletContext().getAttribute("areaDAO");
    private ProductDAO pd = (ProductDAO) ServletActionContext.getServletContext().getAttribute("productDAO");
    private LoanverificationDAO lvd = (LoanverificationDAO) ServletActionContext.getServletContext().getAttribute("loanverificationDAO");
    private RequestDAO requestDAO = (RequestDAO) ServletActionContext.getServletContext().getAttribute("requestDAO");
    private DocumentDAO documentDAO = (DocumentDAO) ServletActionContext.getServletContext().getAttribute("documentDAO");
    private String loanverificationcode;
    private String borid;//borrowerid if renew
    private String loanverid;
    private String loantype;
    private String loanclassification;//productid
    private String loanterm;
    private String amountapplied;
    private String firstname;
    private String middlename;
    private String lastname;
    private String documenttype;
    private String documentdate;
    private String documentno;
    private String status;
    private String selectedArea;//areacode
    private String streetaddress;
    private String barangay;
    private String cityormunicipality;
    private String province;
    private String selectedBranch;//branchcode
    private String employees;//empid
    private String successurl;
    private String actionstatus;
    private SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");

    public String getSuccessurl() {
        return successurl;
    }

    public String getActionstatus() {
        return actionstatus;
    }

    public void setActionstatus(String actionstatus) {
        this.actionstatus = actionstatus;
    }

    public void setAmountapplied(String amountapplied) {
        this.amountapplied = amountapplied;
    }

    public void setBarangay(String barangay) {
        this.barangay = barangay;
    }

    public void setBord(BorrowerDAO bord) {
        this.bord = bord;
    }

    public void setCityormunicipality(String cityormunicipality) {
        this.cityormunicipality = cityormunicipality;
    }

    public void setDocumentdate(String documentdate) {
        this.documentdate = documentdate;
    }

    public void setDocumentno(String documentno) {
        this.documentno = documentno;
    }

    public void setDocumenttype(String documenttype) {
        this.documenttype = documenttype;
    }

    public void setEmployees(String employees) {
        this.employees = employees;
    }

    public void setLoanverid(String loanverid) {
        this.loanverid = loanverid;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public void setLoanclassification(String loanclassification) {
        this.loanclassification = loanclassification;
    }

    public void setLoanterm(String loanterm) {
        this.loanterm = loanterm;
    }

    public void setLoantype(String loantype) {
        this.loantype = loantype;
    }

    public void setLoanverificationcode(String loanverificationcode) {
        this.loanverificationcode = loanverificationcode;
    }

    public void setMiddlename(String middlename) {
        this.middlename = middlename;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public void setSelectedArea(String selectedArea) {
        this.selectedArea = selectedArea;
    }

    public void setSelectedBranch(String selectedBranch) {
        this.selectedBranch = selectedBranch;
    }

    public void setStreetaddress(String streetaddress) {
        this.streetaddress = streetaddress;
    }

    public void setBorid(String borid) {
        this.borid = borid;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Action(value = "/update-la", results = {
        @Result(name = "input", location = "/loanapplication.jsp"),
        @Result(name = "success", location = "${successurl}"),
        @Result(name = "error", location = "${successurl}")
    })
    @Override
    public String execute() {
        //retrieve branch, product, area, employees
        //create borrower
        //create loanverification
        successurl = "../../loanprofile-loanapp.jsp?cat=loanappli&id=" + loanverid;
        Loanverification lv = null;
        Products p = null;
        Borrowers b = null;
        Areas a = null;
        try {
            lv = lvd.get(Integer.parseInt(loanverid));
        } catch (Exception e) {
            actionstatus = "Loan application does not exist.";
            return ERROR;
        }

        //check if required fields are not empty
        if (loantype.equals("") || loantype.equals("-1")) {
            actionstatus = "Please specify the loan type.";
            return ERROR;
        }
        if (loanclassification.equals("") || loanclassification.equals("-1")) {
            actionstatus = "Please specify the loan classification.";
            return ERROR;
        }
        if (loanterm.equals("")) {
            actionstatus = "Please specify the loan term.";
            return ERROR;
        } else {
            try {
                Integer.parseInt(loanterm);
            } catch (Exception ex) {
                actionstatus = "Please specify a valid loan term.";
                ex.printStackTrace();
                return ERROR;
            }
        }

        if (amountapplied.equals("")) {
            actionstatus = "Please specify the amount applied.";
            return ERROR;
        } else {
            try {
                Integer.parseInt(amountapplied);
            } catch (Exception ex) {
                actionstatus = "Please specify a valid amount applied.";
                ex.printStackTrace();
                return ERROR;
            }
        }
        if (firstname.equals("")) {
            actionstatus = "Please specify the first name.";
            return ERROR;
        }
        if (middlename.equals("")) {
            actionstatus = "Please specify the middle name.";
            return ERROR;
        }
        if (lastname.equals("")) {
            actionstatus = "Please specify the last name.";
            return ERROR;
        }
        if (documenttype.equals("")) {
            actionstatus = "Please specify the document type.";
            return ERROR;
        }
        if (documentdate.equals("")) {
            actionstatus = "Please specify the document date.";
            return ERROR;
        }
        if (documentno.equals("")) {
            actionstatus = "Please specify the document no.";
            return ERROR;
        }
        if (selectedArea.equals("") || selectedArea.equals("-1")) {
            actionstatus = "Please specify the address' barangay.";
            return ERROR;
        }
        if (streetaddress.equals("")) {
            actionstatus = "Please specify the address' streetaddress.";
            return ERROR;
        }
        if (barangay.equals("")) {
            actionstatus = "Please specify the address' barangay.";
            return ERROR;
        }
        if (cityormunicipality.equals("")) {
            actionstatus = "Please specify the address' cityormunicipality.";
            return ERROR;
        }
        if (province.equals("")) {
            actionstatus = "Please specify the address' province.";
            return ERROR;
        }
        if (selectedBranch.equals("")) {
            actionstatus = "Please specify the branch.";
            return ERROR;
        }
        if (employees.equals("")) {
            actionstatus = "Please specify the employee";
            return ERROR;
        }
        try {
            p = pd.getByProdName(Integer.parseInt(loanclassification));
        } catch (Exception ex) {
            p = null;
            actionstatus = "Error retrieving loan classification.";
            ex.printStackTrace();
            return ERROR;
        }
        try {
            b = bord.get(Integer.parseInt(borid));
        } catch (Exception ex) {
            p = null;
            actionstatus = "Error retrieving borrower.";
            ex.printStackTrace();
            return ERROR;
        }
        try {
            a = ard.getByAreaCode(selectedArea);
        } catch (Exception e) {
            actionstatus = "Error retrieving area.";
            e.printStackTrace();
            return ERROR;
        }

        //update loan application details
        if (!loantype.equals("")) {
            lv.setLoantype(loantype);
        }

        if (!loanterm.equals("")) {
            lv.setLoanterm(Integer.parseInt(loanterm));
        }

        if (!amountapplied.equals("")) {
            lv.setAmountapplied(Integer.parseInt(amountapplied));
        }

        if (!loanclassification.equals("")) {
            lv.setProducts(p);
        }

        lv.setStatus("posted");

        //firstname, middlename, lastname, streetaddress, barangay, cityormunicipality, province

        if (!firstname.equals("")) {
            b.setFirstname(firstname);
        }

        if (!middlename.equals("")) {
            b.setMiddlename(middlename);
        }

        if (!lastname.equals("")) {
            b.setLastname(lastname);
        }

        if (!streetaddress.equals("")) {
            b.setStreetaddress(streetaddress);
        }

        if (!barangay.equals("")) {
            b.setBarangay(barangay);
        }

        if (!cityormunicipality.equals("")) {
            b.setCityormunicipality(cityormunicipality);
        }

        if (!province.equals("")) {
            b.setProvince(province);
        }

        if (!selectedArea.equals("")) {
            b.setAreas(a);
        }

        Branches branch = (Branches) ServletActionContext.getRequest().getSession().getAttribute("branch");


        try {
            //================
//            Request r = null;
//            Request req = new Request();
//            req.setStatus("Verified");
//            req.setRequestdate(formatter.format(new Date()));
//            req.setBranches(branch);
//            req.setEmployees((Employees) ServletActionContext.getRequest().getSession().getAttribute("employee"));
//            requestDAO.insert(req);
//
//            Documents doc = new Documents();
//            doc.setDocumenttype("Loan Request");
//            doc.setDocumentdate(formatter.format(new Date()));
//            doc.setDocumentno(branch.getBranchcode() + req.getRequestid());
//            doc.setIsactive(true);
//            documentDAO.insert(doc);
//
//            req.setDocuments(doc);
//            requestDAO.update(req);
//            r = req;

            lv.setStatus("Posted");
//            lv.setRequest(r);
            lvd.update(lv);
            //================
//            lvd.update(lv);
            bord.update(b);
            actionstatus = "Loan application posted!";
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }


//        borrower, employee, document

    }
}